package com.ruoyi.quartz.task;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.HttpUrlConstants;
import com.ruoyi.quartz.domain.CePolicy;
import com.ruoyi.quartz.mapper.CePolicyMapper;
import com.ruoyi.quartz.mapper.StockBasicMapper;
import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.parser.PageParser;
import com.xuxueli.crawler.parser.strategy.NonPageParser;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    private static final String policyUrl = "https://hqzc.wssp.hainan.gov.cn/lron/v2/bones/th/search/unifySearch?enabled=Y&sort=22&page=pageNums&pageSize=30&total=2276&type=policy&totalPage=0&ignoreError=Y";

    private static final String policyDetail = "https://hqzc.wssp.hainan.gov.cn/#/policy/detail/";
    @Resource
    private CePolicyMapper cePolicyMapper;

    @Resource
    private StockBasicMapper stockBasicMapper;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(Integer pageNums)
    {
        System.out.println("执行有参方法：" + pageNums);

        String[] urls = new String[pageNums];

        urls[0] = "https://hqzc.wssp.hainan.gov.cn/lron/v2/bones/th/search/unifySearch?enabled=Y&sort=22&page=23&pageSize=30&total=2276&type=policy&totalPage=0&ignoreError=Y";
        urls[1] = "https://hqzc.wssp.hainan.gov.cn/lron/v2/bones/th/search/unifySearch?enabled=Y&sort=22&page=31&pageSize=30&total=2276&type=policy&totalPage=0&ignoreError=Y";
        urls[2] = "https://hqzc.wssp.hainan.gov.cn/lron/v2/bones/th/search/unifySearch?enabled=Y&sort=22&page=35&pageSize=30&total=2276&type=policy&totalPage=0&ignoreError=Y";
        urls[3] = "https://hqzc.wssp.hainan.gov.cn/lron/v2/bones/th/search/unifySearch?enabled=Y&sort=22&page=36&pageSize=30&total=2276&type=policy&totalPage=0&ignoreError=Y";
        urls[4] = "https://hqzc.wssp.hainan.gov.cn/lron/v2/bones/th/search/unifySearch?enabled=Y&sort=22&page=48&pageSize=30&total=2276&type=policy&totalPage=0&ignoreError=Y";
        urls[5] = "https://hqzc.wssp.hainan.gov.cn/lron/v2/bones/th/search/unifySearch?enabled=Y&sort=22&page=58&pageSize=30&total=2276&type=policy&totalPage=0&ignoreError=Y";
//        String tmpUrl = "";
//        int j = 0;
//
//        for (int i =0;i<urls.length;i++){
//            j = i+1;
//            tmpUrl = policyUrl.replaceAll("pageNums",""+j);
//
//            urls[i] = tmpUrl;
//        }


        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(10000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONObject result = JSON.parseObject(pageSource);
                        JSONObject data = result.getJSONObject("data");
                        if (data!=null){
                            JSONArray list = data.getJSONArray("list");
                            JSONObject tmp = null;
                            CePolicy cePolicy = null;
                            for (int i=0;i<list.size();i++){
                                tmp = list.getJSONObject(i);
                                cePolicy = new CePolicy();
                                cePolicy.setPolicyName(tmp.getString("title"));
                                cePolicy.setPolicyContent(tmp.getString("content"));
                                cePolicy.setPublishUnit(tmp.getString("govName"));
                                cePolicy.setPolicyTime(tmp.getString("publishDate"));
                                cePolicy.setCreater(tmp.getString("govId"));
                                cePolicy.setRemark(policyDetail+tmp.getString("id"));
                                cePolicyMapper.insertCePolicy(cePolicy);
                            }
                        }



                    }
                })
                .build();
        crawler.start(true);

    }

    public void ryNoParams(String tsCode)
    {
        System.out.println("执行无参方法");
        String urls = HttpUrlConstants.SINA_STOCK_PERFORMANCE_INDEX_URL.replaceAll("tsCode",tsCode);

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {

                        String resultString = pageSource.substring(pageSource.indexOf("{"),pageSource.lastIndexOf(")"));
                        JSONObject result = JSON.parseObject(resultString);
                        JSONObject data = result.getJSONObject("data");
                        JSONObject reportList = data.getJSONObject("report_list");
                        JSONArray reportDates = data.getJSONArray("report_date");
                        JSONArray reportData = null;
                        JSONObject reportDate = null;
                        JSONObject report = null;

                        for (int i=0;i<reportDates.size();i++){
                            reportDate = reportDates.getJSONObject(i);
                            report = reportList.getJSONObject(reportDate.getString("date_value"));
                            reportData = report.getJSONArray("data");
                        }

                    }
                })
                .build();
        crawler.start(true);
    }

    public void ryPage(String tsCode,String startDate,String endDate){

        String url = HttpUrlConstants.SINA_STOCK_HISTORY_DIST_UTL.replaceAll("tsCode",tsCode).replaceAll("startDate",startDate).replaceAll("endDate",endDate);
        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(url)
                .setPauseMillis(1000)
                .setPageParser(new PageParser() {
                    @Override
                    public void parse(Document document, Element element, Object o) {

                        Element element1 = document.getElementById("datalist");

                        Element tbody = element1.child(1);

                        Elements list = tbody.children();

                        for (Element e :list){
                            System.out.println(e.child(0).html());
                            System.out.println(e.child(1).html());
                            System.out.println(e.child(2).html());
                        }



                    }
                })
                .build();

        crawler.start(true);
    }

}
